package com.hongxun.budget.ui.dialog;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.EPointEntityDao;
import com.hongxun.budget.ui.activity.ys.CheckSetMainActivity;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.ViewUtils;
import com.hongxun.greenentity.ys.EPointEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.yuyh.library.imgsel.widget.DividerGridItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/31.
 */

public class MarkerYsClickDialog extends BaseCenterFragmentDialog {

    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.icon)
    ImageView icon;
    EPointEntity d;


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_marker_click;
    }

    @Override
    protected void initViews() {
        ViewUtils.setIcon(d, icon);
        code.setText("(主点位)" + d.getPPCode());
        List<EPointEntity> entities = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().queryBuilder()
                .where(EPointEntityDao.Properties.PPParentguid.eq(d.getGuid()), EPointEntityDao.Properties.PPDatastate.notEq("删除")).build().list();
        list.setLayoutManager(RecyclerViewUtils.getGridLayoutManager(context, 3));
        list.addItemDecoration(new DividerGridItemDecoration(context));
        SuperAdapter<EPointEntity> adapter;
        list.setAdapter(adapter = new SuperAdapter<EPointEntity>(R.layout.content_markerclickdialog) {
            @Override
            public void bindView(ViewHolder viewHolder, EPointEntity EPointEntity, int i) {
                ImageView icon = viewHolder.getView(R.id.icon);
                TextView code = viewHolder.getView(R.id.code);
                ViewUtils.setIcon(EPointEntity, icon);
                code.setText(EPointEntity.getPPCode());
            }
        });
        adapter.setData(entities);
        adapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<EPointEntity>() {
            @Override
            public void onClick(int i, EPointEntity EPointEntity) {
                pointClick(EPointEntity);
                dismiss();
            }
        });
    }

    public MarkerYsClickDialog setD(EPointEntity d) {
        this.d = d;
        return this;
    }

    void pointClick(EPointEntity d) {
        Intent intent = new Intent(context, CheckSetMainActivity.class);
        intent.putExtra("point", d);
        context.startActivity(intent);
    }

    @OnClick({R.id.cancel, R.id.icon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                dismiss();
                break;
            case R.id.icon:
                pointClick(d);
                break;
        }
    }
}
